<template>
	<view style="padding-top: 40upx;">
		<view class="inputArea">
			<input v-model="phone_number" placeholder="请输入手机号(国内)" type="number" maxlength="11" class="inputClass" />
		</view>
		<!-- <view class="inputArea">
			<view style="display: flex;">
				<input type="number" maxlength="6" placeholder="短信验证码" class="inputClass" style="flex:4;border-radius: 22px 0 0 22px;"
				 v-model="registerCode" />
				<view class="inputClass" @click="getsmscode" style="flex:1;border-radius:0 22px 22px 0;border-left: none;">{{smsbtn.text}}</view>
			</view>
		</view> -->
		<view class="inputArea">
			<input v-model="password" placeholder="密码(至少符号数字大小写两种)" type="password" class="inputClass" />
		</view>
		<view class="inputArea">
			<input v-model="confirmation_password" placeholder="确认登录密码" type="password" class="inputClass" />
		</view>
		<view style="padding: 0 10%;">
			<text style="color: red;">{{message}}</text>
		</view>
		<view class="inputArea">
			<button style="border-radius:22px;" type="primary" @click="goRegister">注 册</button>
		</view>
	</view>
</template>

<script>
	import { register} from '@/common/api'
	export default {
		data() {
			return {
				phone_number: '',
				password: '',
				confirmation_password: '',
				registerCode: '',
				smsbtn: {
					text: '发送',
					status: false,
					codeTime: 60
				},
				timerId: null,
				message: ''
			}
		},
		onLoad() {
			// console.log('register页面onLoad');
		},
		methods: {
			getsmscode() {
				//此处写发送验证码逻辑
				if (this.smsbtn.codeTime != 60) {
					return;
				}
				this.timerId = setInterval(() => {//此处直接复制了另一个插件里的计时器，在插件市场里搜索登录，时间最靠前的那位
						let codeTime = this.smsbtn.codeTime;
						codeTime--;
						this.smsbtn.codeTime = codeTime;
						this.smsbtn.text = codeTime + "S";
						if (codeTime < 1) {
							clearInterval(this.timerId);
							this.smsbtn.text = "重试";
							this.smsbtn.codeTime = 60;
							this.smsbtn.status = false;
						}
					},
					1000);
				return false;
			},
			goRegister() {
				let phone_number = this.phone_number;
				let password = this.password;
				let confirmation_password = this.confirmation_password;
				// let registerCode = this.registerCode;
				let hash = 'acbcefg';
				let timestamp = Math.round(new Date().getTime() / 1000).toString();
				if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(phone_number))) {
					this.message = "手机号码有误，请重填";
					return false;
				}
				// if (registerCode < 100000) {
					// this.message = "验证码不符合格式";
					// return false;
				// }
				if (!password) {
					this.message = "密码不能为空";
					return false;
				}

				let ls = 0;
				if (password.match(/([a-z])+/)) {
					ls++;
				}
				if (password.match(/([0-9])+/)) {
					ls++;
				}
				if (password.match(/([A-Z])+/)) {
					ls++;
				}
				if (password.match(/[^a-zA-Z0-9]+/)) {
					ls++;
				}
				if (password.length < 8) {
					ls = 0;
				}
				if (ls < 2) {
					this.message = "密码强度不够，至少8位，大写、小写、字母、符号 其中两种";
					return false;
				}


				if (confirmation_password != password) {
					this.message = "两次密码不同";
					return false;
				}
				uni.showLoading({
					title: '加载中。。。',
					mask: false
				});
				
				register({phone_number:phone_number,password:password,confirmation_password:confirmation_password,hash:hash,timestamp:timestamp}).then(res=>{
					
					if(res.code == '200'){
						uni.hideLoading();
						uni.showToast({
							title: '注册成功',//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
						//用户信息
						uni.setStorageSync('userInfo',JSON.stringify(res.data.user_info));
						//公式信息
						let formula = res.data.private_formula;
						formula.sort(function(a, b) {
						    return a.updated_at > b.updated_at ? -1 : 1;
						});
						uni.setStorageSync('formula',JSON.stringify(formula));
						setTimeout(function () {
							uni.switchTab({
								url: '../index/index'
							});
						}, 2000);
					}else{
						uni.hideLoading();
						uni.showToast({
							title: res.msg,//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
						return
					}
				}).catch(
					(response) => {
						uni.hideLoading();
						uni.showToast({
							title: "系统错误"+response.message,//标题 必填
							icon: 'none',//图标
						    mask: false,//是否显示透明蒙层，防止触摸穿透，默认：false
						    duration: 2000,//提示的延迟时间，单位毫秒，默认：1500
						});
						
					}
				)
			},
			openAgreement() {
				uni.navigateTo({
					url: '../login/userAgreement',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>

<style>
	.inputArea {
		padding: 20upx 10%;
	}

	.inputClass {
		border: 2px solid #ccc;
		border-radius: 22px;
		outline: 0;
		padding: 8px 15px;
	}
</style>
